<template>
  <div class="div">
    <div class="div1">
      <model :queList="queList" :butFlag="butFlag" :rules="rules" :items="items" :options="opti"
             :addForms="addForms" :tabIndex="tabIndex" :editableTabsValue2="editableTabsValue2" @val="val" @ad="ad"
             @index="index" :editableTabs2="editableTabs2" @query="query" @addTab="addTab" :querys="querys"/>
    </div>
    <commtab :items="items" :butFlag="butFlag" :list="list" :loading="loading" :rules="rules" :high="high"
             :options="opti" @show="show" :editForm="editForm" @del="del" @val="val" @ad="ad"
             :editableTabsValue2="editableTabsValue2" :tabIndex="tabIndex" @index="index"
             :editableTabs2="editableTabs2" @editTab="editTab"/>
    <el-col :md="{span: 5, offset: 6}" :lg="{span: 4, offset: 10}">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[20, 30, 40, 50]"
        :page-size="pagesize"
        :current-page="page"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-col>
  </div>
</template>
<script>
  import Commtab from '../components/Commtab.vue'
  import Model from '../components/Model.vue'
  import axios from 'axios'
  import moment from 'moment'
  import {mapState} from 'vuex'
  export default{
    name: 'Newstab',
    components: {
      Model,
      Commtab
    },
    props: {
      editableTabsValue2: String,
      tabIndex: Number,
      editableTabs2: Array
    },
    data () {
      return {
        list: [],
        high: 0,
        loading: false,
        butFlag: true,
        items: [
          {
            prop: 'id',
            label: '编号',
            add: false,
            table: false,
            edit: false
          },
          {
            prop: 'title',
            label: '主标题',
            add: true,
            table: true,
            edit: true
          },
          {
            prop: 'subTitle',
            label: '副标题',
            add: true,
            table: true,
            edit: true
          },
          {
            prop: 'modifyDate',
            label: '最近更新日期',
            add: false,
            table: true,
            edit: false,
            meth: function (row, column) {
              var date = row[column.property]
              if (date === null) {
                return date
              } else {
                return moment(date).format('YYYY-MM-DD HH:mm:ss')
              }
            },
            dateSelect: '0'
          },
          {
            prop: 'status',
            label: '是否在新闻资讯显示',
            add: true,
            table: true,
            edit: true,
            meth: function (row, column) {
              var state = row[column.property]
              if (state === false) {
                state = '下线'
              } else {
                state = '上线'
              }
              return state
            },
            options: [{
              value: true,
              label: '上线'
            },
            {
              value: false,
              label: '下线'
            }]
          },
          {
            prop: 'content',
            label: '新闻内容',
            add: true,
            table: false,
            edit: true,
            content: '0'
          },
          {
            prop: 'orders',
            label: '显示顺序',
            add: true,
            table: false,
            edit: true
          }
        ],
        rules: {
          title: [
            {required: true, message: '请填写标题', trigger: 'blur'},
            {min: 1, max: 32, message: '长度在 1 到 32 个字', trigger: 'blur'}
          ],
          content: [
            {required: true, message: '请编辑新闻', trigger: 'blur'}
          ],
          status: [
            {type: 'boolean',required: true, message: '请选择状态', trigger: 'blur'}
          ],
          orders: [
            {type: 'number', required: true, message: '请填写顺序', trigger: 'blur'}
          ],
          subTitle:[
            {required: false},
            {min: 1, max: 32, message: '长度在 1 到 32 个字', trigger: 'blur'}
          ]
        },
        queList: [
          {
            width: '3',
            name: '标题',
            prop: 'title',
            i1: '0'
          }],
        page: 1,
        pagesize: 20,
        total: 0,
        opti: [],
        editForm: {
          id: '',
          title: '',
          subTitle: '',
          content: '',
          status: '',
          orders: 0,
        },
        addForm: {
          id: '',
          title: '',
          subTitle: '',
          iconUrl: '',
          content: '',
          status: '',
          orders: 0
        },
        addForms: false,
        title: '',
        querys: {
          title: ''
        }
      }
    },
    mounted () {
      this.get()
      this.high = window.innerHeight - 230
      this.$emit('show1', this.items)
    },
    computed: {
      ...mapState({
        isLogin: state => state.eui.isLogin,
        username: state => state.eui.username
      })
    },
    created () {
      if (!sessionStorage.getItem('username')) {
        this.$router.replace('/')
      }
    },
    methods: {
      handleCurrentChange (val) {
        this.page = val
        this.get()
      },
      handleSizeChange (val) {
        this.pagesize = val
        this.get()
      },
      show (msg) {
        this.editForm.id = msg.id
        this.editForm.title = msg.title
        this.editForm.subTitle = msg.subTitle
        this.editForm.content = msg.content
        this.editForm.status = msg.status
        this.editForm.orders = parseInt(msg.orders)
        this.$emit('show', this.editForm)
        this.$emit('rule', this.rules)
      },
      del (msg) {
        this.loading = true
        axios({
          method: 'get',
          url: `${this.BASE_URL}/news/del`,
          responseType: 'json',
          params: {
            page: this.page,
            pagesize: this.pagesize,
            id: msg.id
          }
        }).then((response) => {
          this.loading = false
          this.total = response.data.total
          this.list = response.data.data
          this.$message('删除成功')
        }).catch(() => {
          this.loading = false
        })
      },
      get () {
        this.loading = true
        axios({
          method: 'get',
          url: `${this.BASE_URL}/news/getAll`,
          responseType: 'json',
          params: {
            page: this.page,
            pagesize: this.pagesize,
            title: this.title
          }
        }).then((response) => {
          this.list = []
          if (response.data.statuCode === '0') {
            this.loading = false
            this.total = response.data.total
            response.data.data.forEach((item) => {
              this.list.push(item)
              this.loading = false
            })
          } else {
            this.loading = false
            this.$alert('没有相关内容', '提示', {
              confirmButtonText: '确定'
            })
          }
        }).catch(() => {
          this.loading = false
        })
      },
      val (msg) {
        this.$emit('val', msg)
      },
      ad (msg) {
        this.$emit('ad', msg)
      },
      index (msg) {
        this.$emit('index', msg)
      },
      query (msg) {
        this.title = msg.title
        this.get()
      },
      addTab (msg) {
        this.$emit('addtab', '')
      },
      editTab (msg) {
        this.$emit('editab', '')
      }
    }
  }
</script>
<style scoped>

  .div1 {
    width: 100%;
    height: 50px;
    padding-top: 10px;
  }

  .el-select-dropdown__item {
    height: auto;
  }
</style>
